package com.ch.web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ch.web.entity.user.XyTalkImagesEntity;
import com.ch.web.mapper.XyTalkImagesDao;
import com.ch.web.service.XyTalkImagesService;
import com.ch.web.utils.PageUtils;
import com.ch.web.utils.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Transactional(rollbackFor = Exception.class)
@Service("xyTalkImagesService")
public class XyTalkImagesServiceImpl extends ServiceImpl<XyTalkImagesDao, XyTalkImagesEntity> implements XyTalkImagesService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<XyTalkImagesEntity> page = this.page(
                new Query<XyTalkImagesEntity>().getPage(params),
                new QueryWrapper<XyTalkImagesEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<XyTalkImagesEntity> getImagesByTalkId(Long talkId) {
        QueryWrapper<XyTalkImagesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("target_talk_id", talkId);
        return baseMapper.selectList(wrapper);
    }

    @Override
    public boolean addTalkImages(Long talkId, List<String> imageUrls) {
        try {
            imageUrls.forEach(s -> {
                this.save(new XyTalkImagesEntity(talkId, s));
            });
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public boolean deleteImagesByTalkId(Long id) {
        QueryWrapper<XyTalkImagesEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("target_talk_id", id);
        if (this.list(wrapper).size() > 0) {
            return this.remove(wrapper);
        } else {
            return true;
        }
    }

}